package it.avantsoft.ls.base;

import it.avantsoft.ls.gl.GroupLeaderHandler;
import it.avantsoft.ls.gl.data.AddressBookManager;
import org.apache.log4j.Logger;
import it.avantsoft.ls.peer.PeerHandler;

import java.util.Set;

public class Application {
    private static Logger log = Logger.getLogger(Application.class);
    private PeerHandler peerHandler;
    private GroupLeaderHandler groupLeaderHandler;
    private Boolean isServer;


    public Application(boolean isServer, int number) {
        AddressBookManager addressBookManager = new AddressBookManager();
        groupLeaderHandler = new GroupLeaderHandler(this, isServer, number, addressBookManager);
        groupLeaderHandler.start();
        this.isServer = isServer;
        if (!isServer) {
            peerHandler = new PeerHandler(this, number, addressBookManager);
            peerHandler.start();
        }
    }

    public PeerHandler getPeerHandler() {
        return peerHandler;
    }

    public GroupLeaderHandler getGroupLeaderHandler() {
        return groupLeaderHandler;
    }

    public void kill() {
        log.info("Killing Peer " + peerHandler.getPeerId());
        groupLeaderHandler.shutdown();
        peerHandler.shutdown();
    }


    public String printLAB() {
        String output = "";
        if (peerHandler != null) {
            if ((peerHandler.getPeerId()) != null) {

                output = peerHandler.getPeerId();

            }

            output += "_{LAB}";
            Set<String> childNames = groupLeaderHandler.getLABSet();
            ApplicationTest.form.addNameSet(output, childNames);
            //output += setToString(childNames);
        } else if (isServer) {
            ApplicationTest.form.addNameSet("Server_{LAB}", groupLeaderHandler.getLABSet());
            //output = "Server_{LAB}" + setToString();
        } else output = this + " : NULL (printing LAB)";

        return output;
    }

    public String printCAB() {
        String output = "";
        if (peerHandler != null) {
            if (peerHandler.getPeerId() != null) {

                output = peerHandler.getPeerId();

            }

            output += "_{CAB}";
            Set<String> childNames = groupLeaderHandler.getCABSet();
            ApplicationTest.form.addNameSet(output, childNames);
            //output += setToString(childNames);
        } else if (isServer)
            output = "Server " + groupLeaderHandler.getCABSet();
        else output = this + " : NULL (printing CAB)";

        return output;
    }

    public String printUAB() {
        String output = "";
        if (peerHandler != null) {
            if (peerHandler.getPeerId() != null) {


                output = peerHandler.getPeerId();

            } else if (isServer)
                output = "Server";

            output += "_{UAB}";
            Set<String> childNames = groupLeaderHandler.getUABSet();
            ApplicationTest.form.addNameSet(output, childNames);
            //output += setToString(childNames);
        } else if (isServer)
            output = "";
        else output = this + " : NULL (printing UAB)";

        return output;
    }

    public String printAPL() {
        String output = "";
        if (peerHandler != null) {
            if (peerHandler.getPeerId() != null) {

                output = peerHandler.getPeerId();

            } else if (isServer)
                output = "Server";

            output += "_{APL}";
            Set<String> childNames = groupLeaderHandler.getAPLSet();
            ApplicationTest.form.addNameSet(output, childNames);
            //output += setToString(childNames);
        } else if (isServer)
            output = "";
        else output = this + " : NULL (printing APL)";
        return output;
    }

    public String printIAP() {
        String output = "";
        Set<String> childNames = groupLeaderHandler.getIAPSet();
        if (childNames.size() > 0) {
            if (peerHandler != null) {
                if (peerHandler.getPeerId() != null) {

                    output = peerHandler.getPeerId();

                } else if (isServer) {
                    output = "Server";
                }

                output += "_{IAP}";
                ApplicationTest.form.addNameSet(output, childNames);
                //output += setToString(childNames);
            } else if (isServer)
                output = "";
            else output = this + " : NULL (printing APL)";
        }
        return output;
    }

//    private String setToString(Set<String> names) {
//        String s = "";
//        if (!names.isEmpty()) {
//            Object[] namesArray = names.toArray();
//            for (int i = 0; i < names.size() && i < Configuration.MAX_GROUP_CHILD_ALLOWED; i++) {
//                Object o = namesArray[i];
//                s += "_" + o;
//
//            }
//        }
//
//        return s;
//    }

    //////////////////

    public static void main(String[] args) {
        boolean isServer = Boolean.parseBoolean(args[0]);
        log.info("isServer = " + isServer);
        new Application(isServer, 999);
    }
}
